package cn.wolfcode.mq;

import cn.wolfcode.service.ISeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "OrderMQFailListener",
        topic = MQConstant.ORDER_RESULT_TOPIC,
        messageModel = MessageModel.BROADCASTING,
        selectorExpression = MQConstant.ORDER_RESULT_FAIL_TAG
)
@Slf4j
public class OrderMQFailListener implements RocketMQListener<OrderMQResult> {

    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        log.info("进入了失败监听");
        // 回补预库存
        seckillProductService.anaplerosis(orderMQResult);
        // 修改本地标识
        rocketMQTemplate.sendOneWay(MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC,orderMQResult.getSeckillId());
    }
}
